package com.gdth.bank.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.greenrobot.eventbus.EventBus;

import com.bumptech.glide.Glide;
import com.gdth.bank.R;
import com.gdth.bank.activity.WebViewActivity;
import com.gdth.bank.event.MessageEvent;
import com.gdth.bank.presenter.NotificationPresenter;
import com.gdth.database.bean.NotificationT;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class NotificationAdapter extends BaseAdapter{
	private Context context;
	private List<NotificationT> listData ;
	private List<NotificationT> SelData;
	private int layout = R.layout.adapter_notification;
	private NotificationPresenter presenter;
	private String sfz;
	private boolean isDel = false;
	
	public NotificationAdapter(Context context, List<NotificationT> listData, NotificationPresenter presenter, String sfz) {
			this.context = context;
			this.listData = listData;
			this.presenter= presenter;
			this.sfz = sfz;
			SelData = new ArrayList<NotificationT>();
	}
	
	public void OnSelDel(boolean isDel) {
		this.isDel = isDel;
		notifyDataSetChanged();
	}
	
	public List<NotificationT>  getSelListData() {
		return SelData;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if(null == listData || listData.size() == 0) {
			return 0;
		}
		return listData.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return listData.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}
	
	public void  setRefresh(List<NotificationT> listData ) {
		if(listData != null && listData.size() > 0) {
			this.listData = listData;
			notifyDataSetChanged();
		}
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		NotificationT datass = null;
		try {
			NotificationT datas = listData.get(position);
			datass = datas;
		}catch (IndexOutOfBoundsException e) {
			// TODO: handle exception
		}
		final NotificationT data = datass;
		
		ViewHolder holder = null;
		if (convertView == null){
			holder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(layout, null);
		    holder.title = (TextView) convertView.findViewById(R.id.notification_title);
		    holder.content = (TextView) convertView.findViewById(R.id.notification_content);
		    holder.backgruod = (LinearLayout)convertView.findViewById(R.id.backgruod);
		    holder.item_notification_dian = (ImageView)convertView.findViewById(R.id.item_notification_dian);
		    holder.sel_id = (ImageView)convertView.findViewById(R.id.sel_id);
			convertView.setTag(holder);
        }
		else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		if(isDel) {
			 holder.sel_id.setVisibility(View.VISIBLE);
		}else {
			 holder.sel_id.setVisibility(View.GONE);
		}
		
		if(SelData.contains(data)) {
			Glide.with(context).load(R.drawable.icon_sel).into(holder.sel_id);
		}else {
			Glide.with(context).load(R.drawable.icon_sel_no).into(holder.sel_id);
		}
		
		 holder.sel_id.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(SelData.contains(data)) {
					SelData.remove(data);
					Glide.with(context).load(R.drawable.icon_sel_no).into((ImageView)v);
				}else {
					SelData.add(data);
					Glide.with(context).load(R.drawable.icon_sel).into((ImageView)v);
				}
			}
		});
	   
    	holder.backgruod.setOnClickListener(new OnClickListener(){		// 点击背景，跳转到详情界面

			@Override
			public void onClick(View v) {
				EventBus.getDefault().post(new MessageEvent(MessageEvent.NOTIFICATION_READ, String.valueOf(data.getXxtsId())));
				Intent intent = new Intent(context, WebViewActivity.class);
                intent.putExtra("path", data.getTzlj());
                intent.putExtra("name", data.getTzcs());
                context.startActivity(intent);
			}
    	});
	   
	   holder.backgruod.setOnLongClickListener(new OnLongClickListener() {

		@Override
		public boolean onLongClick(View v) {
			//delNotification(data.getXxtsId());
			
			return false;
		}
		   
	   });
		
		if (holder != null){
			holder.title.setText(data.getTzbt());
			holder.content.setText(data.getTznr());
			 if (data.getXxzt() == 1){
	               holder.item_notification_dian.setVisibility(View.VISIBLE);
	           }else {
	               holder.item_notification_dian.setVisibility(View.GONE);
	           }
		}
		  
		
		return convertView;
	}

	public static class ViewHolder{
		
		public TextView title = null;
		public TextView content = null;
		public ImageView item_notification_dian = null;
		public LinearLayout  backgruod = null;	
		public ImageView sel_id;
	
	}
	
	private OnCjjlDetailClickListener mOnCjjlDetailClickListener;
	
	public void setOnCjjlDetailClickListener(OnCjjlDetailClickListener mOnCjjlDetailClickListener) {
		this.mOnCjjlDetailClickListener = mOnCjjlDetailClickListener;
	}
	
	public interface OnCjjlDetailClickListener {
		void clickListener(int position);
	}
	
	public void delNotification() {
		//presenter.delNotification(sfz, String.valueOf(xxid)); 
		if(null == SelData || SelData.size() == 0) {
			return ;
		}
		List<Map> selId = new ArrayList<Map>();
		for(int i = 0; i < SelData.size() ; i ++) {
			Map<String, String> map = new HashMap<String, String>();
			map.put("xxid", String.valueOf(SelData.get(i).getXxtsId()));
			selId.add(map);
		}
		presenter.delMoreNotification(sfz,selId);
	}
}
